package com.example.security.library;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Net {

	protected static final String baseUrl = "http://storm-project.fr/secu";

	protected static List<NameValuePair> newListNameValuePair(){
		return new ArrayList<NameValuePair>();
	}

	protected static List<NameValuePair> construireDonnes(Object...donnes){
		List<NameValuePair> donneesPost = new ArrayList<NameValuePair>();
		add(donneesPost,donnes);
		return donneesPost;
	}

	protected static void add(List<NameValuePair> donneesPost, Object...donnes){
		for(int i=0;i<donnes.length;i+=2){
			String cle = donnes[i].toString();
			String valeur = donnes[i+1].toString();
			donneesPost.add(new BasicNameValuePair(cle, valeur));
		}
	}


	protected static String requete(String url, List<NameValuePair> donneesPost){

		HttpClient httpClient = new DefaultHttpClient();
		try {

			String urlPost = baseUrl+url+".php";

			HttpPost requete = new HttpPost(urlPost);

			if(donneesPost != null)
				requete.setEntity(new UrlEncodedFormEntity(donneesPost,"UTF-8"));

			Log.d("NET",urlPost);
			HttpResponse httpReponse = httpClient.execute(requete);

			String response = EntityUtils.toString( httpReponse.getEntity(), HTTP.ISO_8859_1 ).trim().replace("&aecute", "�");  
			Log.d("NET",response);
			return response;
			//}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

		} finally{
			if(httpClient!=null)
				httpClient.getConnectionManager().shutdown();
		}

		Log.e("NET","echec de la requete");
		return null; //en cas d'echec
	}

	public static Action[] connect(String id,String name){
		return parserAction(
				Net.requete("/device/connected", 
						Net.construireDonnes(
								"id",id,
								"name",name
								))
				);
	}

	public static void executed(String deviceId,String actionId){
		Net.requete("/device/executed", 
				Net.construireDonnes(
						"id",deviceId,
						"action",actionId,
						"data", "{}"
						));
	}
	
	public static void executed(String deviceId,String actionId, String data){
		Net.requete("/device/executed", 
				Net.construireDonnes(
						"id",deviceId,
						"action",actionId,
						"data", data
						));
	}

	public static Action[] parserAction(String texte){
		Action actions[] = null;

		try {
			JSONObject json = new JSONObject(texte);

			JSONArray jsonActions = json.getJSONArray("actions");
			actions = new Action[jsonActions.length()];

			for(int i=0;i<jsonActions.length();++i){
				JSONObject jsonAction = jsonActions.getJSONObject(i);

				actions[i] = new Action();

				actions[i].id = jsonAction.getString("id");
				actions[i].type = jsonAction.getString("type");

				JSONObject jsonParams = jsonAction.getJSONObject("params");
				Iterator iter = jsonParams.keys();
				while(iter.hasNext()){
					String key = (String)iter.next();
					String value = jsonParams.getString(key);
					actions[i].params.put(key,value);

				}

			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return actions;
	}
}
